package com.example.content.activity

import android.app.ProgressDialog
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import com.example.content.R
import com.example.content.databinding.ActivityLoginBinding
import com.example.content.viewmodule.login.LoginViewModel
import com.example.core.extensions.showToast
import com.gyf.immersionbar.ImmersionBar
import com.umeng.socialize.UMAuthListener
import com.umeng.socialize.UMShareAPI
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.utils.SocializeUtils

class LoginActivity : BaseActivity<ActivityLoginBinding, LoginViewModel>(), View.OnClickListener {

    private lateinit var dialog:ProgressDialog

    override fun initView() {
        ImmersionBar.with(this).statusBarDarkFont(false).init()
        dialog = ProgressDialog(this)
        viewModel.loginResult.observe(this, Observer<Boolean> {
            if (it) {
                startActivity(Intent(this@LoginActivity, MainActivity::class.java))
                finish()
            } else {
                showToast("登录失败")
            }
        })
    }

    override fun initData() {

    }

    override fun initListener() {
        dataBinding.loginBtn.setOnClickListener(this)
        dataBinding.moreBtn.setOnClickListener {
            this.showToast("暂未支持...敬请期待")
        }
        dataBinding.loginWechart.setOnClickListener {
//            UMShareAPI.get(this).isAuthorize(this, SHARE_MEDIA.WEIXIN)
            UMShareAPI.get(this).doOauthVerify(this, SHARE_MEDIA.WEIXIN, object : UMAuthListener{

                override fun onStart(p0: SHARE_MEDIA?) {
                    SocializeUtils.safeCloseDialog(dialog)
                }

                override fun onComplete(
                    p0: SHARE_MEDIA?,
                    p1: Int,
                    p2: MutableMap<String, String>?
                ) {
                    SocializeUtils.safeCloseDialog(dialog)
                    showToast("授权成功！")
                }

                override fun onError(p0: SHARE_MEDIA?, p1: Int, p2: Throwable?) {
                    SocializeUtils.safeCloseDialog(dialog)
                    showToast("失败: ${p2?.message}")
                }

                override fun onCancel(p0: SHARE_MEDIA?, p1: Int) {
                    SocializeUtils.safeCloseDialog(dialog)
                    SocializeUtils.safeCloseDialog(dialog)
                    showToast("取消了")
                }
            })
        }
        dataBinding.loginQq.setOnClickListener {
            UMShareAPI.get(this).doOauthVerify(this, SHARE_MEDIA.QQ, object : UMAuthListener{

                override fun onStart(p0: SHARE_MEDIA?) {
                    SocializeUtils.safeCloseDialog(dialog)
                }

                override fun onComplete(
                    p0: SHARE_MEDIA?,
                    p1: Int,
                    p2: MutableMap<String, String>?
                ) {
                    SocializeUtils.safeCloseDialog(dialog)
                    showToast("授权成功！")
                }

                override fun onError(p0: SHARE_MEDIA?, p1: Int, p2: Throwable?) {
                    SocializeUtils.safeCloseDialog(dialog)
                    showToast("失败: ${p2?.message}")
                }

                override fun onCancel(p0: SHARE_MEDIA?, p1: Int) {
                    SocializeUtils.safeCloseDialog(dialog)
                    showToast("取消了")
                }
            })
        }
    }

    override fun getLayoutId() = R.layout.activity_login

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.login_btn -> {
                val phoneNumber = dataBinding.phoneNum.text.toString()
                val verifyCode = dataBinding.verifyCode.text.toString()
                viewModel.login(phoneNumber,verifyCode)
            }
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        UMShareAPI.get(this).onSaveInstanceState(outState)
    }

    override fun onDestroy() {
        super.onDestroy()
        UMShareAPI.get(this).release()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        UMShareAPI.get(this).onActivityResult(resultCode, resultCode, data)
    }
}